﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WMS_BLL;
using WMS_DATABASE_MYSQL.MappingClass;
using WMS_IBLL;
using WMS_WEBAPI.Controllers.Base;

namespace WMS_WEBAPI.Controllers.Table
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class AssetsController : ControllerBase
    {
        private readonly ILogger<AccountController> _logger;
        private readonly string controllerName;
        private readonly  IAssetsIBLL _assetsIBLL;
        private readonly ICateGoryIBLL _cateGoryIBLL;
        public AssetsController(ILogger<AccountController> logger, IAssetsIBLL assetsIBLL, ICateGoryIBLL cateGoryIBLL)
        {
            _logger = logger;
            controllerName = GetType().Name;
            _assetsIBLL = assetsIBLL;
            _cateGoryIBLL = cateGoryIBLL;
        }
        //以下为管理员及以上角色才能使用的方法
        //[Authorize(Roles = "superadmin,admin")]
        [HttpGet]
        public async Task<IActionResult> GetAssetsAsync(int current, int pageSize, int? assetsclass, int? assetsname,string? sn,string? assetsnumber,string? type,DateTime? time)
        {
            var data = await _assetsIBLL.ToWherePageListAsync(current, pageSize, a => (assetsclass == null || a.assetsclass == assetsclass) && (assetsname == null || a.assetsname == assetsname)
            && (sn == null || a.sn == sn) && (assetsnumber == null || a.assetsnumber == assetsnumber) && (type == null || a.type == type) && (time == null || a.time <= time));
            var datacount = data.Count();
            return Ok(new { data, total = datacount,suceess=true });
        }
        /// <summary>
        /// 获取指定类别信息，用于前端选择框里的内容生成
        /// </summary>
        /// <param name="categoryclass"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetAssetsCategoryAsync(int categoryclass)
        {
            var data = await _cateGoryIBLL.QueryListAsync(a=>a.category==categoryclass);
            return Ok(data);
        }

        [HttpDelete]
        public async Task<IActionResult> DelAssetsAsync([FromBody] List<int> key)
        {
            var t=await _assetsIBLL.DeleteRangeAsync(a=>key.Contains(a.key));
            if (t > 0) {
                return Ok("成功删除");
            }
            return BadRequest();
        }

        [HttpPut]
        public async Task<IActionResult> EditAssetsAsync([FromBody] wms_assets_table data)
        {
            var t = await _assetsIBLL.EditAsync(data);
            if (t)
            {
                return Ok("成功修改");
            }
            return BadRequest();
        }
        [HttpPost]
        public async Task<IActionResult> AddAssetsAsync([FromBody] wms_assets_table data)
        {
            var t = await _assetsIBLL.CreateAsync(data);
            if (t)
            {
                return Ok("成功添加");
            }
            return BadRequest();
        }
    }
}
